package com.zhb.controller;

/**
 * Created by jarvan4dev on 16/5/23.
 */

import com.zhb.entity.BookInfo;
import com.zhb.exception.BookNotFoundException;
import com.zhb.services.BookService;
import com.zhb.services.UserService;
import com.zhb.util.IOUtils;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 之所以没有叫BookContextController,
 * 是因为发现search部分不需要共享httpClient
 */
public class BookController {

    private final HttpClient httpClient;

    private BookService bookService;

    private UserService userService;

    public BookController(){
        httpClient = HttpClients.createDefault();;
        bookService = new BookService(httpClient);
        userService = new UserService(httpClient);
    }

    public void searchAndSave(String bookName) throws IOException {
        try {
            //userService.doLoginOpac("0004989", "19801025");
            List<BookInfo> resultList = bookService.search(bookName);
            IOUtils.saveResult(resultList, "searchResult.json");
        } catch (BookNotFoundException e){
            // 检索不到则会抛异常
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        BookController controller = new BookController();
        String bookName = IOUtils.getInput("请输入要搜索的书名");
        controller.searchAndSave(bookName);
    }
}
